const req = require("express/lib/request");
const db = require("../db");

const bcrypt = require("bcryptjs")
exports.register = (req, res) => {
    const userinfo = req.body
    if (!userinfo.username || !userinfo.password) {
        return res.send({ status: 1, message: '用户名或密码不合法！' })
    }
    const str1 = 'select * from ev_users where username = ?'
    db.query(str1, userinfo.username, (err, results) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        if (results.results > 0) {
            return res.send({ status: 1, message: '用户名被占用，请更换其他用户名！' })
        }
        userinfo.password = bcrypt.hashSync(userinfo.password, 10)
        console.log(userinfo);
        const str2 = 'insert into ev_users set ?'
        db.query(str2, { username: userinfo.username, password: userinfo.password }, (err, results) => {
            if (err) return res.send({ status: 1, message: err.message })
            if (results.affectedRows !== 1) {
                return res.send({ status: 1, message: '注册用户失败，请稍后再试！' })

            }
            res.send({
                status: 0,
                message: '注册成功！'
            })
        })

    })
};
exports.login = (req, res) => { res.send('login OK') }